/**
 *
 */
package com.ssyt.examinationsystem.service.impl;

import com.ssyt.examinationsystem.dao.IRoleInfoDAO;
import com.ssyt.examinationsystem.dao.IStudentInfoDAO;
import com.ssyt.examinationsystem.dao.impl.RoleInfoDAOImpl;
import com.ssyt.examinationsystem.dao.impl.StudentInfoDAOImpl;
import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.entity.StudentInfo;
import com.ssyt.examinationsystem.service.IStudentInfoService;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 业务层实现类
 *
 * @author Administrator
 */
public class StudentInfoServiceImpl implements IStudentInfoService {
    private IStudentInfoDAO stuDao = new StudentInfoDAOImpl();
    private IRoleInfoDAO roleDao = new RoleInfoDAOImpl();

    /**
     * 登录的业务方法
     *
     * @param user
     * @return
     */
    public boolean login(StudentInfo stu) {
        return stuDao.login(stu);
    }

    @Override
    public List<StudentInfo> getStudentInfos() {
        // 调用数据处理层的查询方法
        List<Map<String, Object>> list = stuDao.getStudentInfos();
        // 创建一个存放学生对象的集合
        List<StudentInfo> stuList = new ArrayList<StudentInfo>();
        if (CollectionUtils.isEmptyForList(list)) {
            for (Map<String, Object> map : list) {
                // 创建一个学生对象，并封装属性值
                StudentInfo stu = new StudentInfo();
                stu.setStuId(Integer.valueOf(String.valueOf(map.get("stuId"))));
                stu.setAccount(String.valueOf(map.get("account")));
                stu.setPassword(String.valueOf(map.get("password")));
                stu.setStuClass(String.valueOf(map.get("stuClass")));
                RoleInfo role = new RoleInfo();
                String roleId = String.valueOf(map.get("roleId"));
                // 根据角色编号的到角色对象
                Map<String, Object> mapRole = roleDao.getRoleByRoleId(roleId);
                role.setRoleName(String.valueOf(mapRole.get("roleName")));
                role.setRoleId(Integer.valueOf(roleId));
                stu.setRole(role);
                stu.setStuName(String.valueOf(map.get("stuName")));
                // 把封装好的学生对象添加到集合中
                stuList.add(stu);
            }
        }
        return stuList;
    }

    @Override
    public int deleteStudentInfo(String stuId) {
        return stuDao.deleteStudentInfo(stuId);
    }

    @Override
    public int addStudentInfo(StudentInfo stu) {
        return stuDao.addStudentInfo(stu);
    }

    @Override
    public int updateStudentInfo(StudentInfo stu) {
        return stuDao.updateStudentInfo(stu);
    }

    @Override
    public Map<String, Object> getStuByStuId(String stuId) {
        return stuDao.getStuByStuId(stuId);
    }

    @Override
    public Map<String, Object> getStuByAcc(String account, String password) {
        return stuDao.getStuByAcc(account, password);
    }

}
